package xiaomage360droidplug.com.a360droidplug

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.util.Log
import com.morgoo.droidplugin.pm.PluginManager
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.util.TreeSet

class MainActivity : AppCompatActivity() {

    private val TAG = "MyActivity"
    private val mFragmentStatePagerAdapter = object : FragmentStatePagerAdapter(supportFragmentManager) {

        override fun getCount(): Int {
            return 2
        }

        override fun getItem(position: Int): Fragment {
            return if (position == 0) {
                InstalledFragment()
            } else {
                ApkFragment()
            }
        }

        override fun getPageTitle(position: Int): CharSequence {
            return if (position == 0) {
                "已安装"
            } else {
                "待安装"
            }
        }
    }

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mViewPager.adapter = mFragmentStatePagerAdapter
        getPerms();
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    private fun getPerms() {
        val pm = packageManager
        val pkgs = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS)
        object : Thread() {
            override fun run() {
                try {
                    val sb = StringBuilder()
                    com.morgoo.helper.Log.e(TAG, "===========包权限start===========")
                    val ps = TreeSet<String>()
                    for (pkg in pkgs) {
                        if (pkg.permissions != null && pkg.permissions.size > 0) {
                            for (permission in pkg.permissions) {
                                ps.add(permission.name)
                            }
                        }
                    }
                    for (p in ps) {
                        val permission = pm.getPermissionInfo(p, 0)
                        val pkg = pm.getPackageInfo(permission.packageName, 0)
                        val re = String.format("<uses-permission android:name=\"%s\"/>", permission.name)
                        val ms = String.format("%s,%s,%s,%s,%s,%s,%s,%s", permission.packageName, pkg.applicationInfo.loadLabel(pm), permission.name, permission.group, permission.protectionLevel, permission.loadLabel(pm), permission.loadDescription(pm), re)
                        sb.append(ms).append("\r\n")
                        com.morgoo.helper.Log.e(TAG, "packageName=%s, name=%s group=%s protectionLevel=%s", permission.packageName, permission.name, permission.group, permission.protectionLevel)
                    }

                    var w: FileWriter? = null
                    try {
                        w = FileWriter(File(Environment.getExternalStorageDirectory(), "per.txt"))
                        w.write(sb.toString())

                    } catch (e: IOException) {
                        e.printStackTrace()
                    } finally {
                        if (w != null) {
                            try {
                                w.close()
                            } catch (e: IOException) {
                            }

                        }
                    }
                    com.morgoo.helper.Log.e(TAG, "===========包权限end===========")
                } catch (e: PackageManager.NameNotFoundException) {
                    e.printStackTrace()
                }

            }
        }.start()
    }
}
